package com.hht.baselibs.layoutManager

import android.view.View
import androidx.recyclerview.widget.LinearSmoothScroller
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.SmoothScroller.ScrollVectorProvider
import androidx.recyclerview.widget.SnapHelper
import com.hht.baselibs.layoutManager.RecycleViewLayoutManager
import com.hht.baselibs.layoutManager.RecycleViewPagerConfig
import com.hht.baselibs.layoutManager.RecycleViewSmoothScroller
import kotlin.math.abs


/**
 * Author:chen'hu
 * Time:2023/8/10 14:07
 * Description:分页居中工具,每次只滚动一个页面
 */
class RecycleViewSnapHelper : SnapHelper() {
    private lateinit var mRecyclerView: RecyclerView


    override fun attachToRecyclerView(recyclerView: RecyclerView?) {
        super.attachToRecyclerView(recyclerView)
        this.mRecyclerView = recyclerView!!
    }

    /**
     * 计算需要滚动的向量，用于页面自动回滚对齐
     * @param layoutManager 布局管理器
     * @param targetView    目标控件
     * @return 需要滚动的距离
     */
    override fun calculateDistanceToFinalSnap(
        layoutManager: RecyclerView.LayoutManager, targetView: View
    ): IntArray? {
        val pos = layoutManager.getPosition(targetView)
        var offset = IntArray(2)
        if (layoutManager is RecycleViewLayoutManager) {
            val manager: RecycleViewLayoutManager = layoutManager
            offset = manager.getSnapOffset(pos)!!
        }
        return offset
    }

    /**
     * 获得需要对齐的View，对于分页布局来说，就是页面第一个
     * @param layoutManager 布局管理器
     * @return 目标控件
     */
    override fun findSnapView(layoutManager: RecyclerView.LayoutManager?): View? {
        if (layoutManager is RecycleViewLayoutManager) {
            val manager: RecycleViewLayoutManager = layoutManager
            return manager.findSnapView()
        }
        return null
    }


    /**
     * 获取目标控件的位置下标
     * (获取滚动后第一个View的下标)
     * @param layoutManager 布局管理器
     * @param velocityX     X 轴滚动速率
     * @param velocityY     Y 轴滚动速率
     * @return 目标控件的下标
     */
    override fun findTargetSnapPosition(
        layoutManager: RecyclerView.LayoutManager?, velocityX: Int, velocityY: Int
    ): Int {
        var target = RecyclerView.NO_POSITION
//        "findTargetSnapPosition, velocityX = $velocityX, velocityY$velocityY".showLog(1)
        if (null != layoutManager && layoutManager is RecycleViewLayoutManager) {
            val manager: RecycleViewLayoutManager = layoutManager
            if (manager.canScrollHorizontally()) {
                if (velocityX > RecycleViewPagerConfig.getFlingThreshold()) {
                    target = manager.findNextPageFirstPos()
                } else if (velocityX < -RecycleViewPagerConfig.getFlingThreshold()) {
                    target = manager.findPrePageFirstPos()
                }
            } else if (manager.canScrollVertically()) {
                if (velocityY > RecycleViewPagerConfig.getFlingThreshold()) {
                    target = manager.findNextPageFirstPos()
                } else if (velocityY < -RecycleViewPagerConfig.getFlingThreshold()) {
                    target = manager.findPrePageFirstPos()
                }
            }
        }
//        "findTargetSnapPosition, target = $target".showLog(2)
        return target
    }

    /**
     * (快速滚动)
     * @param velocityX X 轴滚动速率
     * @param velocityY Y 轴滚动速率
     * @return 是否消费该事件
     */
    override fun onFling(velocityX: Int, velocityY: Int): Boolean {
        val layoutManager = mRecyclerView.layoutManager ?: return false
        val adapter = mRecyclerView.adapter ?: return false
        val minFlingVelocity: Int = RecycleViewPagerConfig.getFlingThreshold()
//        "minFlingVelocity = $minFlingVelocity".showLog(1)
        return ((abs(velocityY) > minFlingVelocity || abs(velocityX) > minFlingVelocity)
                && snapFromFling(layoutManager, velocityX, velocityY))
    }

    /**
     * 快速滚动的具体处理方案
     *
     * @param layoutManager 布局管理器
     * @param velocityX     X 轴滚动速率
     * @param velocityY     Y 轴滚动速率
     * @return 是否消费该事件
     */
    private fun snapFromFling(
        layoutManager: RecyclerView.LayoutManager, velocityX: Int,
        velocityY: Int
    ): Boolean {
        if (layoutManager !is ScrollVectorProvider) {
            return false
        }
        val smoothScroller = createSnapScroller(layoutManager) ?: return false
        val targetPosition = findTargetSnapPosition(layoutManager, velocityX, velocityY)
        if (targetPosition == RecyclerView.NO_POSITION) {
            return false
        }
        smoothScroller.targetPosition = targetPosition
        layoutManager.startSmoothScroll(smoothScroller)
        return true
    }

    /**
     * 通过自定义 LinearSmoothScroller 来控制速度
     * @param layoutManager
     * @return 自定义 LinearSmoothScroller
     */
    override fun createSnapScroller(layoutManager: RecyclerView.LayoutManager?): LinearSmoothScroller? {
        return if (layoutManager !is ScrollVectorProvider) {
            null
        } else RecycleViewSmoothScroller(mRecyclerView)
    }

    /**
     * 设置滚动阀值
     * @param threshold 滚动阀值
     */
    fun setFlingThreshold(threshold: Int) {
        RecycleViewPagerConfig.setFlingThreshold(threshold)
    }
}